<?php

namespace App\Kuafu\Order\Service;

use App\Kuafu\Activity\Enum\ActivityEnum;
use App\Kuafu\Activity\Model\ActivityModel;
use App\Kuafu\Base\Service\BaseService;
use App\Kuafu\Category\Service\CategoryService;
use App\Kuafu\Common\Service\CommonService;
use App\Kuafu\Coupon\Enum\CouponEnum;
use App\Kuafu\Coupon\Service\CouponService;
use App\Kuafu\CouponExt\Enum\CouponExtCodeEnum;
use App\Kuafu\CouponExt\Enum\CouponExtEnum;
use App\Kuafu\CouponExt\Model\OrderCouponExtSplitModel;
use App\Kuafu\CouponPackage\Enum\CouponPackageEnum;
use App\Kuafu\CouponPackage\Model\CouponPackageOrderModel;
use App\Kuafu\Customer\Model\CustomerModel;
use App\Kuafu\Order\Enum\CodeEnum;
use App\Kuafu\Order\Enum\OrderCodeEnum;
use App\Kuafu\Order\Enum\CodeEnum as OrderEnum;
use App\Kuafu\Order\Enum\OrderInfoEnum;
use App\Kuafu\Order\Exceptions\OrderException;
use App\Kuafu\Order\Model\ConsumerOrder;
use App\Kuafu\Order\Model\OrderCouponSplitModel;
use App\Kuafu\Order\Model\OrderDetailSplitModel;
use App\Kuafu\Order\Model\OrderDiscountSplitModel;
use App\Kuafu\Order\Model\OrderModel;
use App\Kuafu\Order\Model\OrderOperateRecordModel;
use App\Kuafu\Order\Model\OrderSplitModel;
use App\Kuafu\Pay\BasePay;
use App\Kuafu\Pay\Enum\PayCodeEnum;
use App\Kuafu\Pay\Exceptions\PayException;
use App\Kuafu\Pay\Model\PayStreamModel;
use App\Kuafu\Pay\Model\RefundStreamModel;
use App\Kuafu\Shop\Enum\ShopCodeEnum;
use App\Kuafu\Shop\Exceptions\ShopException;
use App\Kuafu\Shop\Model\Shop;
use App\Kuafu\Shop\Service\ShopService;
use App\Kuafu\User\Service\AuthService;
use App\Kuafu\WaiMai\Enum\WaiMaiCodeEnum;
use App\Kuafu\WaiMai\Enum\WaiMaiDBEnum;
use App\Kuafu\WaiMai\Exceptions\WaiMaiException;
use App\Kuafu\WaiMai\Model\WaiMaiOrderModel;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use PHP_CodeSniffer\Reports\Code;

class OrderBrokerageService extends BaseService
{
    /**
     * 获取存储佣金的佣金总和
     *
     * @param int $startAt
     * @param int $endAt
     * @param int|null $divisionBillId
     * @return mixed
     */
    public function getBrokerageSum(int $startAt, int $endAt, ?int $divisionBillId = 0)
    {
        return OrderSplitModel::suffix($this->shopId)
                              ->where('shopId', $this->shopId)
                              ->where('orderStatus', CodeEnum::ORDER_STATUS_FINISHED)
                              ->whereBetween('completeTime', [$startAt, $endAt])
                              ->when($divisionBillId !== null, function ($query) use ($divisionBillId) {
                                  return $query->where('divisionBillId', $divisionBillId);
                              })
                              ->sum('brokerage');
    }

    /**
     * 更新外卖订单的分佣ID
     *
     * @param int $startAt
     * @param int $endAt
     * @param int|null $divisionBillId
     * @param int|null $sourceType
     * @return mixed
     */
    public function updateDivisionBillId(int $startAt, int $endAt, int $divisionBillId)
    {
        return OrderSplitModel::suffix($this->shopId)
                              ->where('shopId', $this->shopId)
                              ->where('orderStatus', CodeEnum::ORDER_STATUS_FINISHED)
                              ->whereBetween('completeTime', [$startAt, $endAt])
                              ->update([
                                  'divisionBillId' => $divisionBillId
                              ]);
    }
}

